<?php
namespace Tongcheng\Controller;
use Think\Controller;

/**
 * Class BooksBackController
 * @package Tongcheng\Controller
 *
 * 系统通知管理
 */
class XiTongBackController extends CommonController {
    /**
     * 系统通知管理列表
     */
    public function index(){

        $model = M("BackXitong");
        $map = array();
//        $map['wid'] = 2;//小程序id，固定2
//        $map['status'] = 1;
        // $map['is_pay'] = 1;

        $res['count'] = $model->where($map)->count();
        $page = $_GET['page']?$_GET['page']:1;
        $limit = $_GET['limit']?$_GET['limit']:10;
        $list = $model->field("tp_back_xitong.*,FROM_UNIXTIME(tp_back_xitong.add_time,\"%m-%d %H:%i\") time")->where()->page("{$page},{$limit}")->order('id desc')->select();
        $res['list']= $list;
        if(IS_AJAX){
            $this->ajaxReturn($res);
        }else{
            $this->assign('res', $res);
            $this->display();
        }
    }

    /**
     * 查看系统通知
     */
    public function edit(){
        $booksM = M("BackXitong");
        $id = $_GET['id'];
        $list = $booksM->where('id = '.$id)->find();
        $this->assign('list', $list);
        $this->display();
    }

    public function add(){
        $this->display();
    }

    /**
     * 保存系统通知
     */
    public function save(){
        $id = I('id');
        $_POST['add_time'] = time();
        if(empty($id)){
            $result = M('BackXitong')->add($_POST);
        }else{
            $result = M('BackXitong')->where('id = '.$id)->save($_POST);
        }
        if($result){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }

    /**
     * 删除系统通知
     */
    public function del(){
        $id = I('id');
        if(empty($id)){
            $this->ajaxReturn(0);
        }
        $result = M('BackXitong')->where('id = '.$id)->delete();
        if($result){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }

    /**
     *
     *  后台上传图片
     */
    public function uploadFile(){
        $code = $_FILES['file'];
        if(empty($code)){
            $this->ajaxReturn(0);
        }
        $result = $this->upload($code);
        if(null == $result){
            $this->ajaxReturn(0);
        }else{
            $this->ajaxReturn(array('url'=>$result),'json');
        }
    }

    /**
     * @param $code
     * @return null|string
     * 上传图片
     */
    protected function upload($code){
        date_default_timezone_set("Asia/Shanghai"); //设置时区
        if(is_uploaded_file($_FILES['file']['tmp_name'])) {
            //把文件转存到你希望的目录（不要使用copy函数）
            $uploaded_file=$_FILES['file']['tmp_name'];
            $username = "min_img";
            //我们给每个用户动态的创建一个文件夹
            $user_path=$_SERVER['DOCUMENT_ROOT']."/m_houtai/".$username;
            //判断该用户文件夹是否已经有这个文件夹
            if(!file_exists($user_path)) {
                mkdir($user_path);
            }
            //$move_to_file=$user_path."/".$_FILES['file']['name'];
            $file_true_name=$_FILES['file']['name'];
            $newfilename = time().rand(1,1000)."-".date("Y-m-d").substr($file_true_name,strrpos($file_true_name,"."));
            $move_to_file=$user_path."/".$newfilename;//strrops($file_true,".")查找“.”在字符串中最后一次出现的位置
            //echo "$uploaded_file   $move_to_file";
            if(move_uploaded_file($uploaded_file,$move_to_file)) {
                return 'http://'.$_SERVER['HTTP_HOST'].'/m_houtai/min_img/'.$newfilename;
            } else {
                return null;
            }
        } else {
            return null;
        }

    }
}